//一些公共函数

import Cookies from 'js-cookie'
import {getCurrentInstance} from 'vue'

const userDataKey = 'userData';
const http = 'http://api.linwine.space/api/sjk'

//移除用户信息
export function removeUserData(){
    return Cookies.remove(userDataKey)
}

//清除所有的cookie值
export function clearAllCookie() {
    var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
    if(keys) {
        for(var i = keys.length; i--;)
            document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
    }
}

//获取用户信息
export function getUserData(){
    if(Cookies.get(userDataKey) == undefined)
        return undefined;
    return JSON.parse(Cookies.get(userDataKey))
}


//设置自动登录用户信息,一年以内自动登录
//设置非自动登录
export function setUserData(userData){
    removeUserData()
    if(userData.autoLogin == true)      //自动登录
        return Cookies.set(userDataKey, JSON.stringify(userData), {expires: 365})
    return Cookies.set(userDataKey, JSON.stringify(userData))
}

//头像更新重设置
export function setAvatar(avatar){
    let userData = JSON.parse(Cookies.get(userDataKey))
    console.log(userData);
    userData.avatar = avatar;
    console.log(avatar)
    if(userData.autoLogin)              //自动登录的情况
        return Cookies.set(userDataKey, JSON.stringify(userData), {expires: 365});
    else    
        return Cookies.set(userDataKey, JSON.stringify(userData)) 
}

// 获得用户信息
// 获得用户信息
let userData = {
    Token: '',
    avatar: '',
    id: '',
    role: ''
}
if (getUserData() != undefined) {
    userData = getUserData();
}

// 导出表格
export function exportTable (role){
    let name
    if(role == 1)
        name = '学生'
    else
        name = '教师'
    fetch(http + '/admin/users/excel?role='+role, {
        headers: {
            Token: userData.Token,
            'Content-Type': 'application/json; charset=utf-8'
        },
        method: 'get',
        responseType: "blob",
    }).then((response) => {
        return response.blob()
    }).then((data)=>{
        console.log(data);
        // 创建一个 a 标签，并隐藏 a 标签
        let a = document.createElement("a");
        a.style.display = "none";
        // 对象 URL 绑定到 a 标签上
        a.href = window.URL.createObjectURL(new Blob([data]));
        // 给 a 标签添加 download 属性 与 文件名
        a.setAttribute("download", `${name}名单${new Date().toLocaleString()}.xls`);
        // 把 a 标签挂载到 dom 树上并执行 a 标签的 click 事件
        document.body.appendChild(a);
        a.click();
        // 释放我们创建的 url 和 a 标签
        window.URL.revokeObjectURL(a.href);
        document.body.removeChild(a);
    })
}


export function deleteUser(id){
    fetch(http + '/admin/subUser/'+id, {
        method: 'delete',
        headers: {
            "Token": userData.Token,
            'Content-Type': 'application/json; charset=utf-8'
        },
        responseType: 'json',
    }).then((response) => {
        return response.json()
    }).then((data) => {
        console.log(data);
        if (data.code == 200) {

            history.go(0)
            return true
        } else {
            return data.msg
        }
    }).catch((err) => {
        console.log(err);
    })
}
